package com.codi.fcloud.ufx.hssale.portfolio;

import com.codi.base.log.annotation.Loggable;
import com.codi.base.util.ExceptionUtil;
import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.exception.HundsunAPIException;
import com.codi.fcloud.ufx.UfxSDKFactory;
import com.codi.fcloud.ufx.constant.HSInputParameterNames;
import com.codi.fcloud.ufx.constant.UrlConstants;
import com.codi.trade.dto.HsCombRedeemTradeDto;
import com.codi.trade.service.portfolio.HsCombRedeemTradeService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 模块名
 *
 * @author spy
 * @version 1.0 2017-12-11 9:46
 * @since 1.0
 */
@Slf4j
@Service
public class HsCombRedeemTradeServiceImpl extends UfxSDKFactory implements HsCombRedeemTradeService {
    @Override
    @Loggable
    public Map<String, Object> redeem(HsCombRedeemTradeDto dto) {

        Preconditions.checkNotNull(dto);


        String              trustWay = apiValueMap.get(HSInputParameterNames.TRUST_WAY);
        Map<String, Object> request  = new HashMap<>();

        request.put("trust_way", trustWay);
        request.put("net_no", getString(dto.getNet_no(), "0001"));

        request.put("trade_acco", getString(dto.getTrade_acco()));//	C17	交易账号	N
        request.put("ta_acco", getString(dto.getTa_acco())); //	C12	TA账号	N
        request.put("password", getString(dto.getPassword())); //	C50	密码	N	传加密后的密文,默认MD5(加密后密文是32位)加密方式
        request.put("apply_time", getString(dto.getApply_time())); //	C6	申请时间	N
        request.put("order_date", getString(dto.getOrder_date())); //	C8	下单日期	N
        request.put("trade_flag", getString(dto.getTrade_flag())); //	C1	交易标识	N
        request.put("portfolio_code", getString(dto.getPortfolio_code())); //	C4	投资组合编码	N
        request.put("fund_exceed_flag", getString(dto.getFund_exceed_flag(),GlobalConstant.FUND_EXCEED_FLAG)); //	C1	巨额赎回标志	N
        request.put("discount_rate", getString(dto.getDiscount_rate())); //	N5.4	费率折扣	N
        request.put("capital_mode", getString(dto.getCapital_mode())); //	C2	资金方式	N
        request.put("bank_no", getString(dto.getBank_no())); //	C3	银行代码	N
        request.put("bank_account", getString(dto.getBank_account())); //	C28	银行账号	N
        request.put("after_discount_rate", getString(dto.getAfter_discount_rate())); //	N5.4	后收费折扣率	N
        request.put("broker", getString(dto.getBroker())); //	C8	经纪人	N
        request.put("returnflag", getString(dto.getReturnflag())); //	C1	交易退款标志	N
        request.put("trade_conduct_person", getString(dto.getTrade_conduct_person())); //	C20	交易经办人	N
        request.put("precheck_flag", getString(dto.getPrecheck_flag())); //	C1	预检查标识	N
        request.put("appointed_days", getString(dto.getAppointed_days())); //	N3	赎回款指定清算天数	N
        request.put("comb_redeem_rate", getString(dto.getComb_redeem_rate())); //	N5.4	组合赎回比例	N
        request.put("channel_serial_id", getString(dto.getChannel_serial_id())); //	C32	银商流水号	N
        request.put("check_no", getString(dto.getCheck_no())); //	C8	主管工号	N
        request.put("check_pwd", getString(dto.getCheck_pwd())); //	C50	主管密码	N
        request.put("proof_no", getString(dto.getProof_no())); //	C24	凭证编号	N

        try {
            Map<String, Object> response = getUfxSDK().transact(getTarget(), request);

            return appendLog(request, response);
        } catch (Exception exception) {
            ExceptionUtil.logError(logger, getTarget(), exception);
            throw new HundsunAPIException(ExceptionUtil.getMessage(exception));
        }
    }

    @Override
    protected String getTarget() {
        return UrlConstants.PREFIX_SALE + UrlConstants.COMB_REDEEM_TRADE;
    }
}
